/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.gov.dna.base.mybatis;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.gov.dna.common.util.GovStringUtils;

/**
 * 扩展分页，默认页数10
 *
 * @author (作者) Dong Jie 154046519@qq.com
 * @version V1.0
 * @date 2018/12/5 16:32
 * @since (该版本支持的JDK版本) 1.8
 */
public class BasePage<T> extends Page<T> {
	/**
	 * 页码
	 */
	private int page;
	/**
	 * 页数
	 */
	private int limit;

	/**
	 * 兼容现有的传值命名，解决坑爹的limit命名和父类重复
	 */
	private boolean hasSetSize = false;

	/**
	 * 开启下划线模式 自动把orderByField转成下划线形式的
	 */
	private boolean underlineMode = true;

	/**
	 *临时记录父类orderByField
	*/
	private String tempOrderByField;

	public BasePage() {
		super(1,10);
		setAsc(false);
	}

	public BasePage(boolean underlineMode) {
		this();
		this.underlineMode=underlineMode;
	}

	public void setPage(int page) {
		this.page = page;
		setCurrent(page);
	}

	public void setLimit(int limit) {
		this.limit = limit;
		if (!hasSetSize) {
			setSize(this.limit);
			hasSetSize = true;
		}
	}

	public boolean isUnderlineMode() {
		return underlineMode;
	}

	public void setUnderlineMode(boolean underlineMode) {
		this.underlineMode = underlineMode;
		if(null!=tempOrderByField){
			setOrderByField(tempOrderByField);
		}
	}

	@Override
	public Pagination setOrderByField(String orderByField) {
		this.tempOrderByField=orderByField;
		if (underlineMode && null != orderByField) {
			orderByField = GovStringUtils.underscoreName(orderByField);
		}else if(underlineMode && null != orderByField){
			orderByField = GovStringUtils.camelCaseName(orderByField);
		}
		return super.setOrderByField(orderByField);
	}
}
